define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";
	// 异步请求
	var wind_async = require('ddf_wind_async')
	var base = require('component-base');
	var map = require('map');
//	require('component-location');
	var history = require('component-history');

	require('component-page');
	var provinceId = "", cityId = "", regionId = "";
	/**
	 * 初始加载页面
	 */

	exports.init = function(m) {
		history.addHis(this, arguments);
		window.$ddf_agentMessage = m;
		var htmlStr = "";
		htmlStr += '<div id="content-header">';
		htmlStr += '<div id="breadcrumb"> <a href=javascript:load("'
				+ ddf.index.index_js
				+ '") title="返回首页" class="tip-bottom"><i class="icon-home"></i> 首页</a><a href="javascript:void(0)" class="current">代理商基本信息</a></div>';
		// htmlStr += '<h1>代理商基本信息</h1>';
		htmlStr += '</div>    ';
		htmlStr += '<div class="container-fluid">';
		// htmlStr += '<hr/>';

		htmlStr += '<div class="row-fluid">';
		htmlStr += '<div class="span12">';
		htmlStr += '<div id="alert-tips2" ></div>';
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title">';
		htmlStr += '<span class="icon">';
		htmlStr += '<i class="icon-pencil"></i>';
		htmlStr += '</span>';
		htmlStr += '<h5>操作</h5>';
		htmlStr += '</div>      ';
		htmlStr += '<div class="widget-content">';
		htmlStr += '<div class="div-btn" id="buttonBox">';
		htmlStr += ' <button class="btn btn-success" id="editBtn" type="button" onClick=$ddf_agentMessage.editAgent()>修改</button> ';

		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '</div>             ';
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title"> <span class="icon"><i class="icon-th"></i></span>';
		htmlStr += '<h5>代理商详情</h5>';
		htmlStr += '</div>';
		htmlStr += '<div class="widget-content nopadding">';
		htmlStr += '<div class="form-horizontal">';
		htmlStr += '<div class="control-group">';
		htmlStr += '<label class="control-label">代理商代码：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<span class="inp_span" id="agentId"></span>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '<div class="control-group">';
		htmlStr += '<label class="control-label">代理商名称：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<span class="inp_span" id="agentName"></span>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		/*
		 * htmlStr += '<div class="control-group">'; htmlStr += '<label
		 * class="control-label">代理商类型：</label>'; htmlStr += '<div
		 * class="controls">'; htmlStr += '<span class="inp_span">' +
		 * data["AGENT_TYPE"]+ '</span>'; htmlStr += '</div>'; htmlStr += '</div>';
		 * 
		 * htmlStr += '<div class="control-group">'; htmlStr += '<label
		 * class="control-label">上级代理商：</label>'; htmlStr += '<div
		 * class="controls">'; htmlStr += '<span
		 * class="inp_span">'+data["PARENT_AGENT_NAME"]+'</span>'; htmlStr += '</div>';
		 * 
		 * htmlStr += '</div>';
		 */
		htmlStr += '<div class="control-group">';
		htmlStr += '<label class="control-label">联系方式：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<span class="inp_span" id="contact"></span>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '<div class="control-group">';
		htmlStr += '<label class="control-label">联系人：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<span class="inp_span" id="contactName"></span>';
		htmlStr += '</div>';
		htmlStr += '</div>          ';
		htmlStr += '<div class="control-group">';
		htmlStr += '<label class="control-label">手机号码：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<span class="inp_span" id="mobile"></span>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '<div class="control-group">';
		htmlStr += '<label class="control-label">联系地址：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<span class="inp_span" id="address"></span>';
		htmlStr += '</div>';
		htmlStr += '</div>          ';

		/*
		htmlStr += '<div class="control-group"><label class="control-label">所在省份：</label><div class="controls"><span class="inp_span" id="provinceName"></span></div></div>';
		htmlStr += '<div class="control-group"><label class="control-label">所在城市：</label><div class="controls"><span class="inp_span" id="cityName"></span></div></div>';
		htmlStr += '<div class="control-group"><label class="control-label">所在区域：</label><div class="controls"><span class="inp_span" id="regionName"></span></div></div>';
		*/
		htmlStr += '<div class="control-group">';
		htmlStr += '<label class="control-label">创建时间：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<span class="inp_span" id="createTime"></span>';
		htmlStr += '</div>';
		htmlStr += '</div>';

		htmlStr += '<div class="control-group">';
		htmlStr += '<label class="control-label">备注：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<span class="inp_span" id="memo"></span>';
		htmlStr += '</div>';
		htmlStr += '</div>';

		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '</div>';

		showDiv("content0");
		$("#content0").html(htmlStr);
		getAgentDetail();
	}

	function getAgentDetail() {
		base.showLoading();
		eval(
				Wind.compile("async", function() {

					var data = $await(wind_async.DDF_AsyncRequest("POST",
							ddf.agentMessage.queryAgent, ""));

					loadAgentDetailCallBack(data);

				}))().start();
	}

	/**
	 * 代理商户的回调函数
	 */
	var agentDataArray = {};
	function loadAgentDetailCallBack(data) {
		if (data["EDIT"] != true) {
			$("#editBtn").css("display", "none");
		}

		if (data["code"] == 1) {

			// 保存到数组中
			agentDataArray["AGENT_ID"] = data["AGENT_ID"];
			agentDataArray["AGENT_NAME"] = data["AGENT_NAME"];
			agentDataArray["AGENT_TYPE"] = data["AGENT_TYPE"];
			agentDataArray["PARENT_AGENT_ID"] = data["PARENT_AGENT_ID"];
			agentDataArray["CONTACT"] = data["CONTACT"];
			agentDataArray["CONTACT_NAME"] = data["CONTACT_NAME"];
			agentDataArray["MOBILE"] = data["MOBILE"];
			agentDataArray["ADDRESS"] = data["ADDRESS"];
			agentDataArray["MEMO"] = data["MEMO"];
			agentDataArray["CREATE_TIME"] = data["CREATE_TIME"];

//			agentDataArray["PROVINCE_NAME"] = data["PROVINCE_NAME"];
//			agentDataArray["CITY_NAME"] = data["CITY_NAME"];
//			agentDataArray["REGION_NAME"] = data["REGION_NAME"];
//			agentDataArray["PROVINCE_ID"] = data["PROVINCE_ID"];
//			agentDataArray["CITY_ID"] = data["CITY_ID"];
//			agentDataArray["REGION_ID"] = data["REGION_ID"];

			$("#agentId").html(agentDataArray["AGENT_ID"]);
			$("#agentName").html(agentDataArray["AGENT_NAME"]);
			$("#contact").html(agentDataArray["CONTACT"]);
			$("#contactName").html(agentDataArray["CONTACT_NAME"]);
			$("#mobile").html(agentDataArray["MOBILE"]);
			$("#address").html(agentDataArray["ADDRESS"]);
			$("#memo").html(agentDataArray["MEMO"]);
			$("#createTime").html(agentDataArray["CREATE_TIME"]);
//			$("#provinceName").html(agentDataArray["PROVINCE_NAME"]);
//			$("#cityName").html(agentDataArray["CITY_NAME"]);
//			$("#regionName").html(agentDataArray["REGION_NAME"]);

			if (updateAgentFlag) {

				base.tips("修改代理商成功");

				updateAgentFlag = false;
			}

		} else {
			base.tips_error(data["message"]);
		}
		base.cancelLoading();
	}

	/**
	 * 修改
	 */

	function editAgent() {
		history.addHis(this, arguments);
//		provinceId = agentDataArray["PROVINCE_ID"];
//		cityId = agentDataArray["CITY_ID"];
//		regionId = agentDataArray["REGION_ID"];

		base.showLoading();

		var htmlStr = "";

		htmlStr += '<div id="content-header">';
		htmlStr += '<div id="breadcrumb"> <a href=javascript:load("'
				+ ddf.index.index_js
				+ '") title="返回首页" class="tip-bottom"><i class="icon-home"></i> 首页</a><a href=javascript:showDiv("content0")>代理商基本信息</a><a href="javascript:void(0)" class="current">编辑代理商信息</a></div>';
//		htmlStr += '<h1>编辑代理商信息</h1>';
		htmlStr += '</div> ';
		htmlStr += '<div class="container-fluid">';
//		htmlStr += '<hr/>';
		htmlStr += '<div class="row-fluid">';
		htmlStr += '<div class="span12">';
//		htmlStr += '<div id="alert-tips1" ></div>';
//		htmlStr += '<div id="errorMessage-editAgent" style="display:none" class="alert alert-error alert-block">';
//		htmlStr += '<a class="close" href="#" data-dismiss="alert">×</a>';
//		htmlStr += '<h4 class="alert-heading">Error!</h4>';
//		htmlStr += 'You are not looking too good. Nulla vitae elit libero, a pharetra augue. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.';
//		htmlStr += '</div>  ';

		
		htmlStr += '<div class="widget-box">';
		htmlStr += '<div class="widget-title"> <span class="icon"><i class="icon-th"></i></span>';
		htmlStr += '<h5>修改代理商</h5>';
		htmlStr += '</div>';
		htmlStr += '<div class="widget-content nopadding">';
		htmlStr += '<form class="form-horizontal">	   ';
		// htmlStr += '<div class="control-group"><label
		// class="control-label">代理商代码：</label><div class="controls"><span
		// class="inp_span" id="edit_agentId">'+AGENT_ID+'</span></div></div>';
		htmlStr += '<div class="control-group" id="control-group-agentName">';
		htmlStr += '<label class="control-label">代理商名称：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span9" type="text" id="input-agentName" value="'
				+ agentDataArray["AGENT_NAME"] + '">';
		htmlStr += '</div>';
		htmlStr += '</div>';

		htmlStr += '<div class="control-group" id="control-group-contact">';
		htmlStr += '<label class="control-label">联系方式：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span9" type="text" id="input-contact" value="'
				+ agentDataArray["CONTACT"] + '">       ';
		htmlStr += '</div>                    ';
		htmlStr += '</div>';
		htmlStr += '<div class="control-group" id="control-group-contactName">';
		htmlStr += '<label class="control-label">联系人：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span9" type="text" id="input-contactName" value="'
				+ agentDataArray["CONTACT_NAME"] + '">';
		htmlStr += '</div>                  ';
		htmlStr += '</div>';
		htmlStr += '<div class="control-group" id="control-group-mobile">';
		htmlStr += '<label class="control-label">手机号码：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span9" type="text" id="input-mobile" value="'
				+ agentDataArray["MOBILE"] + '">';
		htmlStr += '</div>';
		htmlStr += '</div>';

		/*
		htmlStr += '<div class="control-group" id="control-group-area">';
		htmlStr += '<label class="control-label">省/市/区：</label>';
		htmlStr += '<div id="locationShowDiv-edit"></div>';
		*/

		htmlStr += '<div class="control-group" id="control-group-address">';
		htmlStr += '<label class="control-label">联系地址：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span9" type="text" id="input-address" value="'
				+ agentDataArray["ADDRESS"] + '">';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '<div class="control-group" id="control-group-memo">';
		htmlStr += '<label class="control-label">备注：</label>';
		htmlStr += '<div class="controls">';
		htmlStr += '<input class="span9" type="text" id="input-memo" value="'
				+ agentDataArray["MEMO"] + '">';
		htmlStr += '</div>';
		htmlStr += '</div>';
		htmlStr += '<div class="form-actions">';
		htmlStr += '<button class="btn btn-primary" type="button" onclick=$ddf_agentMessage.updateAgent("'
				+ agentDataArray["AGENT_ID"] + '") >保存</button> ';
		htmlStr += '<button class="btn btn-info" type="button" onClick=$ddf_agentMessage.showDiv("content0")>返回</button>  ';
		htmlStr += '</div>  ';
		htmlStr += '</form>';
		htmlStr += '</div>';
		htmlStr += '</div>  ';
		htmlStr += '</div>';
		htmlStr += '</div> ';
		htmlStr += '</div> ';

		// $("#content0").html("");//清除增加代理商的DIV，是为了防止input的ID重复
		//	
		// $("#content2").css("display","none");
		//	
		// $("#content4").css("display","block");
		//	
		// $("#content4").html(htmlStr);
		showDiv("content1");
		$("#content1").html(htmlStr);

		base.cancelLoading();

		// setAgentIdFlag=true;

		// getAgentList("select-agent",PARENT_AGENT_ID);

		/*
		$("#locationShowDiv-edit").ddf_location(
				"agent",
				provinceId,
				cityId,
				regionId,
				{

					setLocation : function(_provinceId, _cityId, _regionId) {
						provinceId = _provinceId;
						cityId = _cityId;
						regionId = _regionId;
						map.allmap("allmap-edit", "", "", $("#province-name")
								.html()
								+ $("#city-name").html()
								+ $("#region-name").html());
					}
				});
		*/
		
		$("#input-agentName").change(
				function() {

					var agentName = $("#input-agentName").val();

					if (isNull(agentName)) {
						addErrorMessage("control-group-agentName",
								"input-agentName", "代理商名称非法");
					} else {
						clearErrorMessage("control-group-agentName");
					}

				});

		$("#input-mobile").change(
				function() {

					var mobile = $("#input-mobile").val();

					if (!isMobile(mobile)) {
						addErrorMessage("control-group-mobile", "input-mobile",
								"手机号码非法");
					} else {
						clearErrorMessage("control-group-mobile");
					}

				});

		$("#input-memo").change(function() {

			var memo = $("#input-memo").val();
			if (isNull(memo)) {
				addErrorMessage("control-group-memo", "input-memo", "备注非法");
			} else {
				clearErrorMessage("control-group-memo");
			}
		});

		$("#input-address").change(
				function() {
					var address = $("#input-address").val();
					if (isNull(address)) {
						addErrorMessage("control-group-address",
								"input-address", "联系地址非法");
					} else {
						clearErrorMessage("control-group-address");
					}
				});

		$("#input-contact").change(
				function() {
					var contact = $("#input-contact").val();
					if (isNull(contact)) {
						addErrorMessage("control-group-contact",
								"input-contact", "联系方式非法");
					} else {
						clearErrorMessage("control-group-contact");
					}
				});

		$("#input-contactName").change(
				function() {
					var contactName = $("#input-contactName").val();
					if (isNull(contactName)) {
						addErrorMessage("control-group-contactName",
								"input-contactName", "联系人非法");
					} else {
						clearErrorMessage("control-group-contactName");
					}
				});

	}

	/**
	 * 修改代理商
	 */

	var updateAgentFlag = false;
	/*
	 * function updateAgentMessage(agentId){ alert("agentId"+agentId); }
	 */

	function updateAgent(agentId) {

		var agentName = $("#input-agentName").val();

		var contact = $("#input-contact").val();

		var contactName = $("#input-contactName").val();

		var mobile = $("#input-mobile").val();

		var address = $("#input-address").val();

		var memo = $("#input-memo").val();

		if (isNull(agentName)) {
			addErrorMessage("control-group-agentName", "input-agentName",
					" 代理商名称非法");
			return;
		}

		if (!isMobile(mobile)) {
			addErrorMessage("control-group-mobile", "input-mobile", "手机号码非法");
			return;
		}

		var params = {
			"agent.agentId" : agentId,
			"agent.agentName" : agentName,
			// "rsAgent.parentAgentId" : parentAgentId,
			"agent.contact" : contact,
			"agent.contactName" : contactName,
			"agent.mobile" : mobile,
			"agent.address" : address,
			"agent.memo" : memo,
//			"agent.provinceId" : provinceId,
//			"agent.cityId" : cityId,
//			"agent.regionId" : regionId

		};

		// alert($.param(params));

		base.showLoading();

		eval(
				Wind.compile("async", function() {

					var data = $await(wind_async.DDF_AsyncRequest("POST",
							ddf.agentMessage.saveAgent, params));

					AfterUpdateAgentCallBack(data);

				}))().start();
	}

	/**
	 * 修改代理商_回调函数
	 */
	function AfterUpdateAgentCallBack(data) {

		if (data["code"] == 1) {
			showDiv("content0");
			updateAgentFlag = true;
			getAgentDetail();
			// base.tips("修改成功！");
		} else {
			updateAgentFlag = false;
			base.tips_error(data["message"]);
		}
		base.cancelLoading();
	}

	/**
	 * 显示DIV
	 */
	function showDiv(block) {

		$("#content > div").hide();

		if ($("#" + block).length <= 0) {

			$("#content").append('<div id="' + block + '"></div>');

		} else {

			$("#" + block).show();
		}

		if (block == "content0") {
			shopTypeId = "";
		}

	}

	exports.editAgent = editAgent; // 修改
	exports.updateAgent = updateAgent;// 更新
	exports.showDiv = showDiv; // 返回
});